package com.tbit.uqbike.push.util;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@Slf4j
public class DateTimeUtil {
    private static SimpleDateFormat sDay = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sDay1 = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat sDay2 = new SimpleDateFormat("yyyyMM");
    private static SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static String forString = "yyyy-MM-dd HH:mm:ss";//格式化样本

    /**
     * 获取当前时间
     */
    public static String getNowTime() {
        return s.format(new Date());
    }

    /**
     * 获取当前时间
     */
    public static String getNowTimeyyyyMMdd() {
        return sDay1.format(new Date());
    }

    /**
     * 获取当前时间(yyyy-MM-dd)
     */
    public static String getNowTimeYMD() {
        return sDay.format(new Date());
    }

    /**
     * 获取指定时间年月
     */
    public static String getYYMM(String dateTime) {
        if (dateTime == null) {
            return sDay2.format(new Date());
        } else {
            try {
                return sDay2.format(s.parse(dateTime));
            } catch (ParseException e) {
                log.error(e.getMessage(), e);
            }

            return sDay2.format(new Date());
        }
    }

    /**
     * 字符串转日期类型
     */
    public static String dateTimeToString(Date d) {
        return s.format(d);
    }

    /**
     * 日期获取当天
     */
    public static int getDayOfMonth(Date date) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取和当前的秒差
     */
    public static Integer getSecondDiffer(String startTime) {
        SimpleDateFormat s = new SimpleDateFormat(forString);
        try {
            Date date1 = s.parse(startTime);
            Date date2 = new Date();

            Long time1 = date1.getTime();
            Long time2 = date2.getTime();

            Long time = time2 - time1;

            return (int) (time / 1000);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return 0;
    }

    /**
     * 根据格林尼治时间格式化
     */
    public static String getDateTimeByGL(Long time) {
        return s.format(time);
    }

}
